/**
 * 
 */
package kr.pe.johnkim.dayve.provider;

import java.util.ArrayList;

import kr.pe.johnkim.dayve.activity.R;
import kr.pe.johnkim.dayve.provider.DayveCategoryAnalysisAdapter.ViewWrapper;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * @author johnkim
 *
 */
public class DayveGroupInformationAdapter extends BaseAdapter {
	
	public static final String TAG = DayveGroupInformationAdapter.class.getSimpleName();
	
	private Context context;
	private ArrayList<DayveCategory> data;
	private DayveCategory item;
	LayoutInflater inf;
	
	View view;
	
	public DayveGroupInformationAdapter(Context context, ArrayList<DayveCategory> data) {
		this.context = context;
		this.data = data;
		inf = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		view = convertView;
		
		ViewWrapper wrapper = null;
		
		if(view == null) {
			view = inf.inflate(R.layout.dayve_group_information_row, parent, false);
			wrapper = new ViewWrapper(view);
			view.setTag(wrapper);
		} else {
			wrapper = (ViewWrapper)view.getTag();
		}
		
		item = data.get(position);
		
		Log.i(TAG, "name        : " + item.getName());
		Log.i(TAG, "items count : " + item.getItemsCount());
		
		wrapper.getTxtViewGroupInformationCategoryName().setText("  " + item.getName() + "  ");
		
		wrapper.getTxtViewGroupInformationCategoryItemCount().setText(item.getItemsCount());
		
		return view;
	}

	/**
	 * 
	 * @author johnkim
	 *
	 */
	class ViewWrapper {
		View base;
		
		TextView txtViewGroupInformationCategoryName;					// 카테고리명.
		TextView txtViewGroupInformationCategoryItemCount;				// 카테고리에 등록된 아이템 총 갯수.
		
		ViewWrapper(View base) {
			this.base = base;
		}
		
		TextView getTxtViewGroupInformationCategoryName() {
			if(txtViewGroupInformationCategoryName == null) {
				txtViewGroupInformationCategoryName = (TextView)view.findViewById(R.id.txtViewGroupInformationCategoryName);
			}
			
			return (txtViewGroupInformationCategoryName);
		}
		
		TextView getTxtViewGroupInformationCategoryItemCount() {
			if(txtViewGroupInformationCategoryItemCount == null) {
				txtViewGroupInformationCategoryItemCount = (TextView)view.findViewById(R.id.txtViewGroupInformationCategoryItemCount);
			}
			
			return (txtViewGroupInformationCategoryItemCount);
		}
	}
}
